package com.zy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zy.entity.Customer;
import com.zy.entity.Dict;
import com.zy.entity.Linkman;
import com.zy.service.CustomerService;
import com.zy.service.LinkManService;
import com.zy.service.impl.CustomerServiceImpl;
import com.zy.service.impl.LinkManServiceImpl;

import sun.awt.image.ImageWatched.Link;

/**
 * Servlet implementation class LinkManServlet
 */
@WebServlet("/LinkManServlet")
public class LinkManServlet extends HttpServlet {
	LinkManService lk = new LinkManServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String control = request.getParameter("control");
		if(control.equals("toadd")) {
			//查询所属客户类型
			List<Customer> customers = lk.getCustomer();
			
			//存域
			request.setAttribute("list", customers);
			request.getRequestDispatcher("/WEB-INF/linkMan/add.jsp").forward(request, response);
		}
		if (control.equals("addLinkMan")) {
			Linkman linkman = new Linkman();
			System.out.println(Integer.parseInt(request.getParameter("clid")));
			linkman.getCustomer().setCid(Integer.parseInt(request.getParameter("clid")));
			linkman.setLinkGender(request.getParameter("linkgender"));
			linkman.setLinkMobile(request.getParameter("linkmobile"));
			linkman.setLinkName(request.getParameter("linkname"));
			linkman.setLinkPhone(request.getParameter("linkphone"));
			
			lk.insertLinkman(linkman);
			
			//此处需要返回联系人列表界面++++++++++++++++++++++++
			response.sendRedirect(request.getContextPath()+"/LinkManServlet?control=list");
			
		}
		if (control.equals("list")) {
			List<Linkman> linkmans = lk.quaryall();
			request.setAttribute("list", linkmans);
			request.getRequestDispatcher("/WEB-INF/linkMan/list.jsp").forward(request, response);
		}
		
		if(control.equals("selectById")) {
			int linkid = Integer.parseInt(request.getParameter("id"));
			Linkman linkman = lk.quaryLinkman(linkid);
			request.setAttribute("linkMan", linkman);

			List<Customer> customers = lk.getCustomer();
			request.setAttribute("list", customers);
			request.getRequestDispatcher("/WEB-INF/linkMan/edit.jsp").forward(request, response);
		}
		if(control.equals("updateLinkMan")) {
			Linkman linkman = new Linkman();
			linkman.setLinkid(Integer.parseInt(request.getParameter("linkid")));
			linkman.setLinkName(request.getParameter("linkname"));
			linkman.setLinkGender(request.getParameter("linkgender"));
			linkman.setLinkPhone(request.getParameter("linkphone"));
			linkman.setLinkMobile(request.getParameter("linkmobile"));
			linkman.getCustomer().setCid(Integer.parseInt(request.getParameter("clid")));
			
			lk.updateLinkman(linkman);
			response.sendRedirect(request.getContextPath()+"/LinkManServlet?control=list");
		}
		if (control.equals("queryByName")) {
			String name = request.getParameter("linkname");
			
			List<Linkman> linkmans = lk.queryByName(name);
			request.setAttribute("list", linkmans);
			request.getRequestDispatcher("/WEB-INF/linkMan/list.jsp").forward(request, response);
		}
		if (control.equals("toDynamicQuery")) {
			//查询所属客户类型
			List<Customer> customers = lk.getCustomer();
			
			//存域
			request.setAttribute("list", customers);
			request.getRequestDispatcher("/WEB-INF/linkMan/select.jsp").forward(request, response);
		}
		if (control.equals("dynamicQuery")) {
			int clid = Integer.parseInt(request.getParameter("clid"));
			String linkname = request.getParameter("linkname");
			List<Linkman> linkmans = new ArrayList<>();
			if (linkname.length()==0) {
				//仅通过所属客户查询
				linkmans = lk.quaryByCustomer(clid);
			}
			if (linkname.length()!=0) {
				//通过所属客户和联系人查询
				linkmans = lk.quaryByCL(clid, linkname);
			}
			request.setAttribute("list", linkmans);
			request.getRequestDispatcher("/WEB-INF/linkMan/list.jsp").forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
